#!/usr/bin/env python

from Modules.Pygmund import Id
from optparse import OptionParser
from os.path import expanduser
import json

h = """\
\n
Id
Cognition Configuration Management.
Creates and manages JSON-based Configuration files.

usage:
	%prog /path/to/file.conf [options] 	
"""

def main():
	o = OptionParser(usage=h, version="%prog 1.0")
	o.add_option("--get", dest="mode", action="store_true", help="Get a Section, or Option", default=True)
	o.add_option("--set", dest="mode", action="store_false", help="Set a Section, or Option")
	o.add_option("-s", "--section", dest="section", help="Configuration Section", default=0, metavar="SECTION")
	o.add_option("-o", "--option", dest="option", help="Configuration Option", default=0, metavar="option")
	o.add_option("-v", "--value", dest="value", help="Value for Option or Section", default=0, metavar="value")

	opts, args = o.parse_args()
	
	if len(args) < 1:
		config = expanduser("id/cognition.conf")
	else:
		config = args[0]
	
	conf = Id.Init(config)
	if conf:
		# Config loaded...
		if opts.mode == True:
			# Get Mode
			r = conf.Get(opts.section, opts.option)
			print json.dumps(r, indent=4)			
		else:
			# Set Mode
			r = conf.Set(opts.section, opts.option, opts.value)
			if r[0]:
				print r[1]
			else:
				o.error("(SET) %s" % r[1])
	
	else:
		o.error("Could not load configuration specified.")

if __name__ == "__main__":
	main()
